<template>
  <div class="div1">
    <div class="login">
      <el-form class="login_form">
        <h2 style="margin-left: 120px;margin-bottom: 20px;color: #303133">{{ msg }}</h2>
        <el-row>
          <el-col>
            <el-input id="el-input" v-model="loginForm.userName" prefix-icon="el-icon-user-solid" clearable
                      placeholder="用户名">
            </el-input>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-input v-model="loginForm.userPassword" prefix-icon="el-icon-key" clearable show-password
                      placeholder="密码">
            </el-input>
          </el-col>
        </el-row>
        <el-button style="width:40%;margin-left: 15px;" @click="handleLogin" type="primary">
          登陆
        </el-button>
        <el-button style="width:40%;margin-left: 50px;" @click="dialogFormVisible = true" type="success">
          注册
        </el-button>
      </el-form>
    </div>

    <el-dialog style="width: 60%;margin-left: 300px" title="用户注册" :visible.sync="dialogFormVisible"
               @close='closeDialog'>
      <el-form :model="loginForm" :rules="rules" ref="loginForm">

        <el-form-item label="用户名称：" prop="userName">
          <el-input v-model="loginForm.userName" autocomplete="off" placeholder="请输入用户名"
                    prefix-icon="el-icon-user-solid" clearable></el-input>
        </el-form-item>

        <el-form-item label="用户密码：" prop="userPassword">
          <el-input v-model="loginForm.userPassword" autocomplete="off" type="password" placeholder="请输入密码"
                    prefix-icon="el-icon-key" clearable show-password></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureRegister">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

export default {
  name: 'LoginSys',
  data() {
    return {
      loginForm: {
        userName: '',
        userPassword: ''
      },
      dialogFormVisible: false,
      msg: '成绩管理系统',
      code: '',
      rules: {
        userName: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }, {pattern: '[A-Za-z0-9_\\-\u4e00-\u9fa5]+', message: '请输入正确的用户名'}],
        userPassword: [{required: true, message: '请输入密码', trigger: 'blur'}, {
          pattern: '^\\d{6,8}$',
          message: '密码必须为6-8位数字'
        }],
      }
    }
  },
  methods: {
    handleLogin() {
      var that = this;
      if (this.loginForm.userName == '' || this.loginForm.userPassword == '') {
        that.$message.error("请输入用户名及密码！！！");
      } else {
        this.$http.post('/users/login', this.loginForm)
            .then(function (response) {
              if (response.data.code == '200') {
                that.$message.success("登录成功！");
                window.sessionStorage.setItem('token', response.data.token);
                that.$router.push({path: '/NavigationBar', query: {userName: response.data.data}});
              } else {
                that.$message.error("用户名或密码错误，请重新输入！！！");
              }
            }).catch(function (error) {
          console.log(error);
          that.$message.error("服务器错误，请稍后重试！！！");
        })
      }
    },
    //关闭弹框
    closeDialog() {
      this.loginForm.userName = '';
      this.loginForm.userPassword = '';
    },
    //确认注册
    sureRegister() {
      var that = this;
      this.dialogFormVisible = false;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$http.post('/users/register', that.loginForm)
              .then(function (response) {
                // console.log(response)
                if (response.data.code == 200) {
                  that.$message.success("注册成功，请登录");
                } else {
                  that.$message.error(response.data.msg);
                }
              }).catch(function (error) {
            console.log(error);
          })
        } else {
          this.$message.error("注册失败，请注意规则")
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.div1 {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url("@/assets/imgs/动态背景图片.gif");
  background-repeat: no-repeat;
  background-size: 100%;
  background-position:-450px -60px;;
}

.login_form {
  margin-top: 150px;
  border: #ccc 1px solid;
  border-radius: 10px;
  width: 26%;
  padding: 1%;
  margin-left: 36%;
  box-shadow: 0px 0px 10px 0px ;
}

>>> .el-input__inner{
  border-radius: 8px;
}

.el-row {
  margin-bottom: 40px;
}

.el-col {
  border-radius: 4px;
}
</style>
